<template>
  <Layout class="p-5 bg-white">
    <Form :label-width="80"
	    :rules="ruleValidate"
	    ref="equipment"
	    :model="equipment"> 
      <FormItem class="w-50" label="设备编号" prop="id">
        <Input v-model="equipment.id" placeholder="请输入设备编号"></Input>
      </FormItem>

      <FormItem class="w-50" label="设备名称" prop="equipmentName">
        <Input v-model="equipment.equipmentName" placeholder="请输入设备名称"></Input>
      </FormItem>
      <FormItem class="w-50" label="设备信息">
        <Input
          v-model="equipment.equipmentInfo"
          placeholder="请输入设备信息"
          type="textarea"
        ></Input>
      </FormItem>

      <FormItem class="text-right">
        <Button @click="submit" type="primary">提交</Button>
        <Button @click="cancel" class="ml-2">取消</Button>
      </FormItem>
    </Form>
  </Layout>
</template>

<script>
export default {
  data() {
    return {
	  ruleValidate: {
	    id: [
	      {
	        required: true,
	        message: 'id不能为空',
	        trigger: 'blur',
	        whitespace: true,
	      },
	    ],
	    equipmentName: [
	      {
	        required: true,
	        message: '名称不能为空',
	        trigger: 'blur',
	        whitespace: true,
	      },
	    ],
	  },
	  equipment:{
		  id: '',
		  equipmentName: '',
		  statu: null,
		  equipmentInfo: '',
	  }
    }
  },
  methods: {
    submit: function() {
	  if (this.$data.equipment.id.trim().length === 0) {
	    this.message.error('设备编号必须输入')
	    return
	  }
	  if (this.$data.equipment.id.indexOf(' ') > -1) {
	    this.message.error('设备编号中不能有空格')
	    return
	  }
	  if (this.$data.equipment.equipmentName.trim().length === 0) {
	    this.message.error('设备名必须输入')
	    return
	  }
	  if (this.$data.equipment.equipmentName.indexOf(' ') > -1) {
	    this.message.error('名称中不能有空格')
	    return
	  }
      var self = this
      //新增设备
      var updateEquipmentPOJO = {
        tableId:this.$route.params.row.tableId,
		id: this.equipment.id,
        equipmentName: this.equipment.equipmentName,
        equipmentInfo: this.equipment.equipmentInfo,
      }

      this.http
        .put({
          url: '/equipment',
          param: updateEquipmentPOJO,
        })
        .then((data) => {
          self.message.info('修改成功')
          this.$tabs.close()
        })
    },
    cancel: function() {
      this.$tabs.close()
    },
  },
  created: function() {
    //根据设备编号查询设备
    this.http
      .get({
        url: '/equipment/info/' + this.$route.params.row.id,
      })
      .then((data) => {
        this.equipment.id = data.id;
        this.equipment.statu = this.statu;
        this.equipment.equipmentName = data.equipmentName;
        this.equipment.equipmentInfo = data.equipmentInfo;
      })	  
  },
  destroyed: function() {
    this.$router.push({
      name: 'machine',
    })
  },
}
</script>

<style></style>
